# coding:utf-8

import requests,json
from util.commonLog import commonLog
import logging
import sys

reload(sys)
sys.setdefaultencoding('utf-8')

logging.setLoggerClass(commonLog)
logger = logging.getLogger(__name__)

def sendRequest(self, url, method, headers, params):

    self.url = url    # 请求地址
    self.method = method    # 请求方式：get post
    self.headers = headers  # 请求头部
    self.params = params    # 请求参数

    if method == 'get':
        response = requests.get(url, headers=headers, params=params) # 发送get请求
        logger.info(u'请求Url是：'+ response.url)
        code = response.status_code
        logger.info(u'返回状态码是：' + str(response.status_code))
        content = json.loads(response.text)
        logger.info(u'返回内容是：' + str(content).decode('utf-8'))
        assert code == 200, u'期待返回码是：{}，实际返回码是：{}'.format(200,response.status_code)
        return code,content

    if method == 'post':
        response = requests.post(url, headers=headers, data=params)  # 发送post请求
        logger.info(u'请求Url是：' + response.url)
        code = response.status_code
        logger.info(u'返回状态码是：' + str(response.status_code))
        content = json.loads(response.text)
        logger.info(u'返回内容是：' + str(content).decode('utf-8'))
        assert code == 200, u'期待返回码是：{}，实际返回码是：{}'.format(200, response.status_code)
        return code,content

    else:
        logger.info(u"请求方式错误,请确认请求方式为get或post中的一种")




